﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationCore;
using System.Drawing;

namespace EnzymeLaborTool
{
    public static class DataGridHelper
    {

        public static void Configure(DataGridView dgv, BatchLayout bl, bool renderfull = true)
        {
            dgv.Rows.Clear();
            dgv.Columns.Clear();
            for (int i = 1; i <= bl.PlateColumnCount; i++)
            {
                var col = new System.Windows.Forms.DataGridViewComboBoxColumn();
                dgv.Columns.Add((i - 1).ToString(), i.ToString());
            }
            foreach (DataGridViewColumn item in dgv.Columns)
            {
                item.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            if (bl.PlateRowCount > 0)
            {

                if (bl.LayoutType == "SPLIT")
                {
                    if (renderfull)
                    {
                        dgv.Rows.Add(bl.PlateRowCount);
                        for (int i = bl.LayoutRowCount; i < dgv.Rows.Count; i++)
                        {
                            dgv.Rows[i].ReadOnly = true;
                            dgv.Rows[i].HeaderCell.Style.BackColor = Color.Orange;
                        }
                    }
                    else
                        dgv.Rows.Add(bl.LayoutRowCount);
                }
                else
                {
                    dgv.Rows.Add(bl.LayoutRowCount);
                }
            }
            foreach (DataGridViewRow row in dgv.Rows)
            {
                row.HeaderCell.Value = row.Index + 1;
            }
        }

        public static void LoadData(DataGridView dgv, BatchLayout bl, string[,] data)
        {
            for (int i = 0; i < data.GetLength(0) && i < dgv.RowCount; i++)
            {
                for (int j = 0; j < data.GetLength(1) && j < dgv.ColumnCount; j++)
                {
                    dgv.Rows[i].Cells[j].Value = data[i, j];
                }
            }
        }


        public static void LoadData(DataGridView dgv, BatchLayout bl)
        {
            DataGridHelper.LoadData(dgv, bl, bl.Layout);
        }

        public static void RepeatRows(DataGridView dgv)
        {
            int check = dgv.RowCount % 2;
            if (check != 0)
                throw new ArgumentException("In order to repeat rows the row count must be devidable by two.");
            int repeatOffset = dgv.RowCount / 2;

            for (int i = 0; i < repeatOffset; i++)
            {
                for (int j = 0; j < dgv.ColumnCount; j++)
                {
                    dgv.Rows[i + repeatOffset].Cells[j].Value = dgv.Rows[i].Cells[j].Value;
                }
            }
        }


    }
}
